package com.example.distinguishcolors;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.example.distinguishcolors.adapter.ColorAdapter;
import com.example.distinguishcolors.adapter.GameRecordAdapter;
import com.example.distinguishcolors.dao.GameRecordDao;
import com.example.distinguishcolors.domain.GameRecord;
import com.example.distinguishcolors.view.LyyDialog;
import com.example.distinguishcolors.view.NoScrollListView;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

public class LeaderboardActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private NoScrollListView lv;
    private LinearLayout resultLl;
    private LinearLayout topLl;
    private List<GameRecord> list=new ArrayList<>();
    private GameRecordAdapter adapter;
    private final GameRecordDao dao=GameRecordDao.getInstance();
    private static final String TAG = "LeaderboardActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaderboard);
        initView();
    }

    private void initView() {
        resultLl=findViewById(R.id.resultLl);
        topLl=findViewById(R.id.topLl);
        toolbar=findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_chevron_left);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {//设置监听要放在setSupportActionBar(toolbar)的后面，不然不起作用
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv=findViewById(R.id.lv);
        ArrayList<GameRecord> query = dao.findAllByOrder();
        Log.d(TAG, "initView: "+query.size());
        if(query.size()==0){
            resultLl.setVisibility(View.VISIBLE);
            topLl.setVisibility(View.GONE);
        }
        list.addAll(query);
        adapter=new GameRecordAdapter(list);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.leaderboard_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if(itemId==R.id.clear){
            LyyDialog lyyDialog=new LyyDialog(this, new LyyDialog.SelectCallBack() {
                @Override
                public void onPositiveClick() {
                    super.onPositiveClick();
                    dao.deleteAll();
                    list.clear();
                    adapter.notifyDataSetChanged();
                    resultLl.setVisibility(View.VISIBLE);
                    topLl.setVisibility(View.GONE);
                }
            },true);
            lyyDialog.setContent("您确定要清空排行榜数据吗？");
            lyyDialog.show();
        }
        return super.onOptionsItemSelected(item);
    }
}